package com.xiao.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiao.service.CreateClassService;
import com.xiao.service.UserService;
import com.xiao.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 创建者：毛
 * 登录注册
 */
@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private CreateClassService createClassService;

    private ObjectMapper objectMapper = new ObjectMapper();

    //注册
    @GetMapping("/reg")
    public Object reg(String uname,String upassword){
        boolean reg = userService.reg(uname, upassword);
        if (reg){
            return "1";
        }else {
            return "-999";
        }
    }

    //登录
    @PostMapping("/login")
    public Object login(String uname,String upassword) throws JsonProcessingException {
        //登录返回一个token
        String token = userService.login(uname, upassword);

        if (token == null){
            return  objectMapper.writeValueAsString(MessageUtil.getMessage(500,"登录失败",null)) ;
        }

        return objectMapper.writeValueAsString(MessageUtil.getMessage(200,token,null));


    }

    //获得所有年级类别
    @PostMapping("/getgradetype")
    public Object getAllGradetype() throws JsonProcessingException {
        return objectMapper.writeValueAsString(createClassService.FindAllGradetype());
    }

    //获得所有学科信息
    @PostMapping("/findsubject")
    public Object findallSubject() throws JsonProcessingException {
        return objectMapper.writeValueAsString(userService.FindSubjectAll());
    }

}
